﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Presenter.Groups;
using Presenter.Users;

namespace SocialNetworkClient.Pages.Group
{
    public partial class GroupMessages : System.Web.UI.Page, IGroupMessagesView
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var groupid = Request.QueryString["id"];

            if (!string.IsNullOrEmpty(groupid))
            {
                if (Session["GroupMessagesPresenter"] != null)
                {
                    var presenter = Session["GroupMessagesPresenter"] as GroupMessagesPresenter;
                    if (presenter != null)
                        presenter.Reload(this, Convert.ToInt32(groupid), (string)Session["Email"], (string)Session["Password"]);
                }
                else
                {
                    Session["GroupMessagesPresenter"] = new GroupMessagesPresenter(this, Convert.ToInt32(groupid), (string)Session["Email"], (string)Session["Password"]);
                }

                CreateDiv.Visible = CanCreateMessages;
            }
            else
            {
                Response.Redirect(@"~/Pages/Error.aspx");
            }
        }

        public void CreateLbButtonClick(object sender, EventArgs e)
        {
            if (Session["Email"] != null && Session["Password"] != null)
            {
                if (EntityCreated != null)
                    EntityCreated((string)Session["Email"], (string)Session["Password"], Convert.ToInt32(Request.QueryString["id"]), TitleTb.Text, TextTb.Text);

                Response.Redirect(@"~/Pages/Group/GroupMessages.aspx?id=" + Request.QueryString["id"]);
            }
        }

        #region Implementation of IGroupMessagesView

        public void GetMessages(List<List<string>> messages)
        {
            MessageTable.Rows.Clear();

            var headerRow = new TableRow();
            headerRow.Cells.Add(new TableCell { Text = "<b>Id</b>" });
            headerRow.Cells.Add(new TableCell { Text = "<b>From</b>" });
            headerRow.Cells.Add(new TableCell { Text = "<b>Title</b>" });
            headerRow.Cells.Add(new TableCell { Text = "<b>Text</b>" });
            headerRow.Cells.Add(new TableCell { Text = "<b>DateTime</b>" });
            MessageTable.Rows.Add(headerRow);

            foreach (var item in messages)
            {
                var row = new TableRow();

                var cell = new TableCell { Text = item[0] };
                row.Cells.Add(cell);

                cell = new TableCell { Text = "<a href='" + Page.ResolveClientUrl("~/Pages/User/Home.aspx") + "?id=" + item[1] + "'>User page</a>" };
                row.Cells.Add(cell);

                cell = new TableCell { Text = item[2] };
                row.Cells.Add(cell);

                cell = new TableCell { Text = item[3] };
                row.Cells.Add(cell);

                cell = new TableCell { Text = item[4] };
                row.Cells.Add(cell);

                MessageTable.Rows.Add(row);
            }
        }

        public event Action<string, string, int, string, string> EntityCreated;

        public bool CanCreateMessages { get; set; }

        #endregion
    }
}